<template>
  <Dialog title="会员分账信息" v-model="visible" width="90%">
    <ContentWrap>
      <el-table
        v-loading="loading"
        :data="list"
        border
        highlight-current-row
        stripe
        @sort-change="sortChange"
      >
        <el-table-column type="selection" width="64" />
        <el-table-column type="index" label="序 号" width="80" align="center" fixed />
        <el-table-column
          sortable="true"
          label="编 号"
          align="center"
          prop="outTradeNo"
          width="300"
        /><el-table-column
          sortable="true"
          label="分账时间"
          align="center"
          prop="createDate"
          width="180"
          :formatter="dateFormatter"
        />
        <el-table-column sortable="true" label="金额(元)" align="right" prop="amount" width="120" />
        <el-table-column
          sortable="true"
          label="描 述"
          align="center"
          prop="description"
          width="120"
        />
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="formData.pageNum"
        v-model:limit="formData.pageSize"
        @pagination="getPage"
      />
    </ContentWrap>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts" name="sharingPageByBuyer">
import { dateFormatter } from '@/utils/formatTime'
import * as Api from '@/api/userCenter/tradeOrder/buyer/profitSharing'
const message = useMessage() // 消息弹窗

const visible = ref(false) // 弹窗的是否展示
const loading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const form = ref({
  id: '',
  name: '',
  picUrl: '',
  description: ''
})
const formRef = ref() // 表单 Ref

const total = ref<number>(0) // 列表的总页数
const list = ref([]) // 列表的数据
const formData = reactive({
  shopBuyerId: '',
  pageNum: 1,
  pageSize: 20,
  searchValue: '',
  filters: '',
  sortOrder: 'DESC', // 排序,asc／desc
  sortName: 'create_date' // 排序,字段
})
/** 打开弹窗 */
const open = async (id?: string) => {
  visible.value = true
  resetForm()
  // 修改时,设置数据
  if (id) {
    loading.value = true
    formData.shopBuyerId = id
    try {
      await getPage()
    } catch (t) {
      console.log(t)
    } finally {
      loading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法,用于打开弹窗

/** 重 置表单 */
const resetForm = () => {
  form.value = {
    id: '',
    name: '',
    picUrl: '',
    description: ''
  }
  formRef.value?.resetFields()
}

/** 查询列表 */
const getPage = async () => {
  loading.value = true
  try {
    const data = await Api.getPage(undefined)
    list.value = data.list
    total.value = data.total
  } catch (t) {
    console.log(t)
    loading.value = false
    message.error('查询列表失败')
  } finally {
    loading.value = false
  }
}

const sortChange = async (data) => {
  formData.sortOrder = data.order.replace(/ending$/, '')
  formData.sortName = data.prop

  getPage()
}
</script>
